package Control;


import Database.DBSalesOrder;
import Model.Customer;
import Model.SalesOrder;

public class SalesOrderCtrl {
    SalesOrder salesOrderModel;

    DBSalesOrder salesOrder = new DBSalesOrder();

    public String createSalesOrder(int orderID, String date, int amount, String deliveryStatus, String deliveryDate, Customer customer) {
	String message = null;
	if (orderID != 0 && date != null && amount != 0 && deliveryStatus != null && deliveryDate != null) {

	    if (salesOrder.findSalesOrder(orderID).getOrderID() != orderID) {
		salesOrderModel = new SalesOrder(orderID, date, amount, deliveryStatus, deliveryDate, customer);

		try {
		    salesOrder.insertSalesOrder(salesOrderModel);
		    message = "Sales Order: " + orderID + " was created succesfuly!";
		} catch (Exception e) {

		    e.printStackTrace();
		}

	    } else {
		// Sent a message to the user that the input was wrong
		message = "Sales Order with this ID[" + orderID + "] already exist!";
	    }
	} else {
	    message = "Please fill up all fields!";

	}
	return message;
    }

    public String findSalesOrder(int orderID) {
	SalesOrder salesOrder = null;
	String message = null;
	try {
	    salesOrder = salesOrder.findSalesOrder(orderID);
	    System.out.println(salesOrder.getOrderID());
	} catch (Exception e) {

	    System.out.println("can't find");
	}

	if (salesOrder.getOrderID() == 0) { // id 0 must not exist

	    message = "Can't find sales order!";
	} else {

	    message = "Sales Order " + salesOrder.getOrderID() + " [Date - " + salesOrder.getDate() + "] was found";
	}
	// Sent a message to the user that the input was wrong or the sales order
	// was not found
	return message;

    }

    public String updateSalesOrder(int orderID, String date, int amount, String deliveryStatus, String deliveryDate, Customer customer) {
	String message = null;
	if (salesOrder.findSalesOrder(orderID).getOrderID() == orderID) {
	    salesOrderModel = new SalesOrder(orderID, date, amount, deliveryStatus, deliveryDate, customer);
	    salesOrder.updateSalesOrder(salesOrderModel);
	    message = orderID + " was updated succesfuly!";
	} else {

	    message = "Sales Order doesn't exist!";
	}
	// We should either prompt for what information to update
	// or if we should force an update in all fields of the salesOrder
	return message;
    }

    public String deleteSalesOrder(int orderID) {

	String message = null;
	if (salesOrder.findSalesOrder(orderID).getOrderID() == orderID) {
	    int salesOrderID = salesOrder.findSalesOrder(orderID).getOrderID();
	    salesOrder.deleteSalesOrder(salesOrderID);

	    message = orderID + " was deleted succesfuly!";
	} else {

	    message = "Sales Order doesn't exist!";
	}
	// Here we need a script that removes the information
	// from the database

	return message;
    }

	

}
